<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

class jZmluvyHelper
  
  {
    function format_bytes($size) {
    $units = array(' B', ' kB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return str_replace('.',',',round($size, 2)).$units[$i];
  }
 
  //*** volitelne zobrazovanie a zadavanie formatu pre datum
  function datdmy($dat) {
  $nastavenie = jZmluvyHelper::nastavenie();
  if($dat == 0) {
  $output = $dat;
  } else {
  $output = date_format(date_create($dat), $nastavenie->datum_format);
  }
  return $output;
  }
  
  //*** spatna konverzia na datum aby bol korektne zapisany do DB  
  function datymd($dat) {
    if($dat == 0) {
  $output = $dat;
  } else {
  $output = date_format(date_create($dat), 'Y-m-d');
  }
  return $output;
  }

  //*** cislo, konverzia bodka<->ciarka, oddelovnik tisicov
  function format_number($cislo,$convert,$tis) {
   // vacsinou len pre zobrazenie
   if($convert == ',' ) {
   $output = number_format($cislo, 2, ',', $tis);
   }
   if($convert == '.' ) {
   // // format pre ulozenie do DB
   $output = str_replace("," , "." , $cislo);
   }
   return $output;
   }
   
   //*** obrazok
  function obrazok($img,$path,$text) {
  $path = 'components'.DS.'com_jzmluvy'.DS.'assets'.DS.$img;
  list($width, $height) = getimagesize($path);
  $image = '<img src="'.$path.'" height="'.$height.'" width="'.$width.'" title="'.$text.'" alt="'.$text.'" />';
  return $image;
  }
  
  function ikona ($extension,$name) {
 
    switch ($extension)
    {
      case 'jpg':
      case 'jpeg':
      case 'jpe':
        $file = 'jpg.gif';
      break;
      case 'gif':
        $file = 'gif.gif';
      break;
      case 'png':
        $file = 'png.gif';
      break;
      case 'rtf':
        $file = 'rtf.gif';
      break;
      case 'txt':
        $file = 'txt.gif';
      break;
      case 'pdf':
        $file = 'pdf.gif';
      break;
      case 'tif':
      case 'tiff':
      case 'jfif':
      case 'jfi':
      case 'jif':
        $file = 'tif.gif';
      break;
      default:
        $file = 'default.gif';      
    }
    
    $image = '<img src="components/com_jzmluvy/assets/images/icon16/'.$file.'" align="middle" title="'.$name.'" alt="'.$extension.'"/>'; 
    return $image;
  }
    
  function pristup() {
    $uzivatel = & JFactory::getUser();
    $params = JComponentHelper::getParams ('com_jzmluvy');
    
    if ($params->get('privilegia') <= $uzivatel->get('gid')) {
      return true;
    }
    return false;
  }
  
  // ulozene nastavenia
  function nastavenie()
	{
		$db =& JFactory::getDBO();
		$query = 'SELECT * FROM #__jzmluvy_nastaveni WHERE id = 1';
		$db->setQuery($query);
		$nastavenie = $db->loadObject();

		return $nastavenie;
	}

  // pri formulari a statistike vytvorenia a modifikacie dokumentu zobrazuje mena 
  function uzivatel($idz)
	{
		$db =& JFactory::getDBO();
		$query  = ' SELECT u.id , juc.name AS vytvoril, jum.name AS zmenil';
    $query .= ' FROM #__jzmluvy AS u';
		$query .= ' LEFT JOIN #__users AS juc ON u.created_by =juc.id';
    $query .= ' LEFT JOIN #__users AS jum ON u.modified_by=jum.id';
    $query .= ' WHERE u.id = '.$idz;
    $db->setQuery($query);
		$meno = $db->loadObject();
    
    return $meno;
	}
  
  	function getVersion() { 
    $folder = JPATH_ADMINISTRATOR .DS. 'components'.DS.'com_jzmluvy';
    if (JFolder::exists($folder)) { 
      $xmlFilesInDir = JFolder::files($folder, '.xml$');
    }
    else {
      $xmlFilesInDir = null;
    }
    
    $xml_items = '';
    if (count($xmlFilesInDir)) {
      foreach ($xmlFilesInDir as $xmlfile) { 
        if ($data = JApplicationHelper::parseXMLInstallFile($folder.DS.$xmlfile)) {
          foreach($data as $key => $value) {
            $xml_items[$key] = $value;
          }
        }
      }
    }
    
    return $xml_items;
  }

    
}

?>